﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using BO.Custom;
using Business;
using Entity;
using Entity.Custom;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;

namespace FreshCms.BackEnd.Controls.Module.Box
{
    public partial class BoxNewContent : System.Web.UI.UserControl
    {
        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion

        public int boxId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindData();
                    writeNavigator();
                    BindCate();
                }
            }
        }

        private void BindData()
        {
            // get box id tu query string
            boxId = Utils.GetQuery("boxid", 0);
        }

        // Hàm write danh sách các box
        private void writeNavigator()
        {
            string filename = Server.MapPath("/") + "BackEnd\\listfile\\boxNew.xml";
            // khoi tao 1 mang dang BoxWeb
            List<BoxWeb> list = new List<BoxWeb>();

            // query vao XML file
            var lst = from c in XElement.Load(filename).Elements("Box")
                      select c;

            // Execute the query 
            foreach (var item in lst)
            {
                BoxWeb obj = new BoxWeb
                {
                    ID = item.Element("ID").Value,
                    Name = item.Element("Name").Value,
                    ImageWidth = item.Element("ImageWidth").Value,
                    ImageHeight = item.Element("ImageHeight").Value
                };
                list.Add(obj);
            }

            // Bind du lieu vao rpt navigator
            rptNavigator.DataSource = list;
            rptNavigator.DataBind();

            // Check neu boxid get tu query string = 0 tuc la chua chon box nao thi mac dinh mo box dau tien trong lisst
            if (boxId == 0) Int32.TryParse(list[0].ID.ToString(), out boxId);

        }


        private void BindCate()
        {
            List<ListItem> lst = new List<ListItem>();
            lst.Add(new ListItem(" -- Tất cả danh mục --", "0"));

            var lstParent = OutCategoryBusiness.GetOutCategoryList(1, 1);
            foreach (OutCategory parent in lstParent)
            {
                lst.Add(new ListItem(parent.CatName, parent.CatId.ToString() + "|0"));
                var lstChild = OutCategoryBusiness.GetOutCategoryChildList(parent.CatId, 1);
                foreach (OutCategory child in lstChild)
                {
                    lst.Add(new ListItem("  ----- " + child.CatName, child.CatId.ToString()));
                }
            }

            ddlCate.Items.AddRange(lst.ToArray());

            // Bind color 
            for (int i = 1; i <= ddlCate.Items.Count - 1; i++)
            {
                try
                {
                    int.Parse(ddlCate.Items[i].Value);
                }
                catch (Exception ex)
                {
                    ddlCate.Items[i].Attributes.Add("style", "color:red;background-color:LightGray;");
                }
            }
        }
    }
}